var onlyOpenTitle="About";//不允许关闭的标签的标题
var navmenus = {
	"menus": [{
		"menuid": "1",
		"icon": "icon-nav",
		"menuname": "系统管理",
		"menus": [{
			"menuid": "11",
			"menuname": "资源",
			"icon": "icon-link",
			"url":"demo.html"
		},
		{
			"menuid": "12",
			"menuname": "角色",
			"icon": "icon-link",
			"url":"demo.html"
		},
		{
			"menuid": "13",
			"menuname": "用户",
			"icon": "icon-link",
			"url":"demo.html"
		},
		{
			"menuid": "14",
			"menuname": "组织",
			"icon": "icon-link",
			"url":"demo.html"
		}]
	},
	{
		"menuid": "2",
		"icon": "icon-nav",
		"menuname": "基础数据",
		"menus": [{
			"menuid": "21",
			"menuname": "客户管理",
			"icon": "icon-link",
			"url": mis.path+ "/page/base/customer.jsp"
		},
		{
			"menuid": "22",
			"menuname": "产品管理",
			"icon": "icon-folder",
			"url": "",
			"child": [{
				"menuid": "221",
				"menuname": "产品类别",
				"icon": "icon-link",
				"url":mis.path+ "/page/base/producttype.jsp"
			},
			{
				"menuid": "222",
				"menuname": "产品",
				"icon": "icon-link",
				"url": mis.path+ "/page/base/product.jsp"
			}]
		}]
	},
	{
		"menuid": "3",
		"icon": "icon-nav",
		"menuname": "销售",
		"menus": [{
			"menuid": "31",
			"menuname": "销售",
			"icon": "icon-folder",
			"url": "",
			"child": [{
				"menuid": "311",
				"menuname": "开单",
				"icon": "icon-link",
				"url": "demo.html"
			},
			{
				"menuid": "312",
				"menuname": "支付",
				"icon": "icon-link",
				"url": "demo.html"
			}]
		},
		{
			"menuid": "32",
			"menuname": "退货",
			"icon": "icon-link",
			"url": "demo.html"
		}]
	}]
};

$(function(){
	InitLeftMenu();
	tabClose();
	tabCloseEven();
})

//初始化左侧accordion与tree菜单
function InitLeftMenu() {
	$("#nav").accordion({animate:true,fit:true,border:false});
	var selectedPanelname = '';
    $.each(navmenus.menus, function(i, n) {
		var menulist ='';
		menulist +='<ul class="navlist">';
        $.each(n.menus, function(j, o) {
			menulist += '<li><div ><a ref="'+o.menuid+'" href="#" rel="' + o.url + '" ><span class="icon '+o.icon+'" >&nbsp;</span><span class="nav">' + o.menuname + '</span></a></div> ';
			if(o.child && o.child.length>0)
			{
				menulist += '<ul class="third_ul">';
				$.each(o.child,function(k,p){
					menulist += '<li><div><a ref="'+p.menuid+'" href="#" rel="' + p.url + '" ><span class="icon '+p.icon+'" >&nbsp;</span><span class="nav">' + p.menuname + '</span></a></div> </li>'
				});
				menulist += '</ul>';
			}
			menulist+='</li>';
        })
		menulist += '</ul>';

		$('#nav').accordion('add', {
            title: n.menuname,
            content: menulist,
			border:false,
            iconCls: 'icon ' + n.icon
        });

		if(i==0)
			selectedPanelname =n.menuname;
    });

	$('#nav').accordion('select',selectedPanelname);
	$('.navlist li a').click(function(){
		var tabTitle = $(this).children('.nav').text();
		var url = $(this).attr("rel");
		var menuid = $(this).attr("ref");
		var icon = $(this).find('.icon').attr('class');
		var third = find(menuid);
		if(third && third.child && third.child.length>0)
		{
			$('.third_ul').slideUp();
			var ul =$(this).parent().next();
			if(ul.is(":hidden"))
				ul.slideDown();
			else
				ul.slideUp();
		}
		else{
			addTab(tabTitle,url,icon);
			$('.navlist li div').removeClass("selected");
			$(this).parent().addClass("selected");
		}
	}).hover(function(){
		$(this).parent().addClass("hover");
	},function(){
		$(this).parent().removeClass("hover");
	});
}

function find(menuid){
	var obj=null;
	$.each(navmenus.menus, function(i, n) {
		 $.each(n.menus, function(j, o) {
		 	if(o.menuid==menuid){
				obj = o;
			}
		 });
	});
	return obj;
}

//添加tab窗口
function addTab(subtitle,url,icon){
	if(!$('#tabs').tabs('exists',subtitle)){
		$('#tabs').tabs('add',{
			title:subtitle,
			content:createFrame(url),
			closable:true,
			icon:icon
		});
	}else{
		$('#tabs').tabs('select',subtitle);
		$('#refresh').click();
	}
	tabClose();
}

//创建tab窗口页面内容
function createFrame(url)
{
	var s = '<iframe scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
	return s;
}

//关闭tab窗口
function tabClose()
{
	/*双击关闭TAB选项卡*/
	$(".tabs-inner").dblclick(function(){
		var subtitle = $(this).children(".tabs-closable").text();
		$('#tabs').tabs('close',subtitle);
	})
	/*为选项卡绑定右键*/
	$(".tabs-inner").bind('contextmenu',function(e){
		$('#tabmenu').menu('show', {
			left: e.pageX,
			top: e.pageY
		});
		var subtitle =$(this).children(".tabs-closable").text();
		$('#tabmenu').data("currtab",subtitle);
		$('#tabs').tabs('select',subtitle);
		return false;
	});
}


//绑定右键菜单事件
function tabCloseEven()
{
    $('#tabmenu').menu({
        onClick: function (item) {
            closeTab(item.id);
        }
    });
    return false;
}

//右键菜单事件处理
function closeTab(action)
{
    var alltabs = $('#tabs').tabs('tabs');
    var currentTab =$('#tabs').tabs('getSelected');
	var allTabtitle = [];
	$.each(alltabs,function(i,n){
		allTabtitle.push($(n).panel('options').title);
	})
    switch (action) {
        case "refresh":
            var iframe = $(currentTab.panel('options').content);
            var src = iframe.attr('src');
            $('#tabs').tabs('update', {
                tab: currentTab,
                options: {
                    content: createFrame(src)
                }
            })
            break;
        case "close":
            var currtab_title = currentTab.panel('options').title;
            $('#tabs').tabs('close', currtab_title);
            break;
        case "closeall":
            $.each(allTabtitle, function (i, n) {
                if (n != onlyOpenTitle){
                    $('#tabs').tabs('close', n);
				}
            });
            break;
        case "closeother":
            var currtab_title = currentTab.panel('options').title;
            $.each(allTabtitle, function (i, n) {
                if (n != currtab_title && n != onlyOpenTitle)
				{
                    $('#tabs').tabs('close', n);
				}
            });
            break;
    }
}

